package com.database.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.database.common.BaseResultCodeEnum;
import com.database.common.ResponseException;
import com.database.common.Tools;
import com.database.dao.FoodMapper;
import com.database.dao.MenuMapper;
import com.database.dao.OrderMapper;
import com.database.dao.ShopKeeperMapper;
import com.database.dao.ShopMapper;
import com.database.dao.StaffMapper;
import com.database.model.Food;
import com.database.model.Menu;
import com.database.model.Order;
import com.database.model.Shop;
import com.database.model.ShopKeeper;
import com.database.model.Staff;
import com.database.service.ShopKeeperService;

@Service("shopKeeperService")
public class ShopKeeperServiceImpl implements ShopKeeperService {
	@Autowired
	private ShopKeeperMapper shopKeeperMapper;
	@Autowired
	private ShopMapper shopMapper;
	@Autowired
	private StaffMapper staffMapper;
	@Autowired
	private FoodMapper foodMapper;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private MenuMapper menuMapper;

	@Override
	public int registeShopKeeper(ShopKeeper shopKeeper) {
		if (shopKeeper.getAccount().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20002);
		} else if (shopKeeper.getContact().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20004);
		} else if (shopKeeper.getName().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20018);
		} else {
			try {
				shopKeeper.setPassword(Tools.getMD5Str(shopKeeper.getPassword()));
				return shopKeeperMapper.insert(shopKeeper);
			} catch (Exception e) {
				throw new ResponseException(BaseResultCodeEnum.ERR_CODE_00001);
			}

		}
	}

	@Override
	public ShopKeeper login(ShopKeeper shopKeeper) {
		if (shopKeeper.getAccount().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20005);
		} else if (shopKeeper.getPassword().equals("")) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20005);
		}
		try {
			boolean value = shopKeeperMapper.selectByAccount(shopKeeper).getPassword().equals(Tools.getMD5Str(shopKeeper.getPassword()));
			if (value) {
				return shopKeeperMapper.selectByAccount(shopKeeper);
			} else {
				throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30000);
			}
		} catch (Exception e) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_30000);
		}

	}

	@Override
	public int registeShop(Shop shop) {
		return shopMapper.insert(shop);
	}

	@Override
	public List<Shop> findShopList(Shop shop) {
		return shopMapper.selectByShopKeeperId(shop);
	}

	@Override
	public int updateShopInfo(Shop shop) {
		int value = shopMapper.updateByKey(shop);
		if (value == 0) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10001);
		}
		return 1;
	}

	@Override
	public int addStaff(Staff staff) {
		int value = staffMapper.insert(staff);
		if (value == 0) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10000);
		}
		return 1;
	}

	@Override
	public int updataStaffInfo(Staff staff) {
		int value = staffMapper.updateByKey(staff);
		if (value == 0) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10001);
		}
		return 1;
	}

	@Override
	public int deleteStaff(Staff staff) {
		int value = staffMapper.deleteByKey(staff);
		if (value == 0) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10002);
		}
		return 1;
	}

	@Override
	public List<Staff> findStaffList(Staff staff) {
		// TODO Auto-generated method stub
		return staffMapper.selectByShopId(staff);
	}

	@Override
	public List<Food> findFoodList(Food food) {
		// TODO Auto-generated method stub
		return foodMapper.selectByShopId(food);
	}

	@Override
	public List<Menu> findOrder(Menu menu) {
		return menuMapper.selectByKey(menu);
	}

	@Override
	public int updataFood(Food food) {
		// TODO Auto-generated method stub
		return foodMapper.updateByKey(food);
	}

	@Override
	public int addFood(Food food) {
		// TODO Auto-generated method stub
		return foodMapper.insert(food);
	}

	@Override
	public int deleteFood(Food food) {
		int value = foodMapper.deleteByKey(food);
		if(value==0){
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_20000);
		}
		return 1;
	}

	@Override
	public List<Order> findOrderList(Order order) {
		// TODO Auto-generated method stub
		return orderMapper.selectByShopId(order);
	}

	@Override
	public int updataOrderStatus(Order order) {
		// TODO Auto-generated method stub
		return orderMapper.updateStatusByKey(order);
	}

	@Override
	public int updataOrderStaff(Order order) {
		// TODO Auto-generated method stub
		return orderMapper.updateStatusByKey(order);
	}

	@Override
	public Shop findShop(Shop shop) {
		Order order = new Order();
		order.setShopId(shop.getShopId());
		Double turnover=orderMapper.selectSumPrice(order);  //算出总营业额
		System.out.println("评分");
		Double rank=orderMapper.selectRank(order);
		System.out.println(rank);
		Integer orderCount=orderMapper.selectCount(order);
		Shop shop2=new Shop();
		shop2.setShopId(shop.getShopId());
		shop2.setOrderCount(orderCount);
		shop2.setTurnover(turnover);
		shop2.setRank(rank);
		shopMapper.updateByKey(shop2);
		return shopMapper.selectByKey(shop);
	}

	@Override
	public Food findFood(Food food) {
		// TODO Auto-generated method stub
		return foodMapper.selectByKey(food);
	}

	@Override
	public ShopKeeper findShopKeeperInfo(ShopKeeper shopKeeper) {
		// TODO Auto-generated method stub
		return shopKeeperMapper.selectByKey(shopKeeper);
	}

	@Override
	public int updateShopKeeperInfo(ShopKeeper shopKeeper) {
		// TODO Auto-generated method stub
		return shopKeeperMapper.updateByKey(shopKeeper);
	}

}
